<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use App\Paper; 
use App\Comment;
use App\Category;

class PaperController extends Controller
{
    //
    public function index(){   
        $papers = Paper::where('result', 1)->orderBy('created_at', 'desc')->get();
        // DB::select('SELECT SUBSTRING(createDate FROM 1 FOR 8)')
        // $papers->body = substr($papers->body, 0, 5);
        return view('paper/index',['papers'=> $papers]);
    }

    public function create(){
        return view('paper/create');
    }

    public function store(Request $request){
        $this->validate($request, [
            'title'=>'required|unique:papers|max:255',
            'body'=>'required',
        ]);

        $paper = new Paper();
        $paper->title = $request->get('title');
        $paper->body = $request->get('body');
        if ($request->hasFile('imgOne')) {
            $file = $request->file('imgOne');
            if($file->isValid()){ 
                $clientName = $file -> getClientOriginalName();    //客户端文件名称..
                $entension = $file -> getClientOriginalExtension();   //上传文件的后缀.
                $newName = $newName = md5(date('ymdhis').$clientName).".".$entension;    //定义上传文件的新名称
                $newName = md5(date("Y-m-d-H-i-s").'-'.$clientName).'.'.$entension;
                $file->move(public_path().'/img/cover', $newName);
            }
        }
        $paper->cover_img = $newName;
        $paper->author = $request->user()->id;

        if($paper->save()){
            return redirect('paper');
        }else{
            return redirect()->withErrors('提交失败！');
        }
    }

    public function edit($id) { 
        $paper = Paper::find($id); 
        return view('paper/edit')->with('paper', $paper); 
    }

    public function show($id){
        $paper = Paper::find($id);
        $paper->read_total += 1;
        $paper->save();
        return view('paper/show')->with('hasManyUsers')->with('hasManyComments')->with('paper', $paper);
    }

    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'title' => 'required|unique:papers,title,'.$id.'|max:255',
            'body' => 'required', 
        ]);

        $paper = Paper::find($id);
        $paper->title = $request->get('title');
        $paper->body = $request->get('body');

        if ($paper->save()) {
            return redirect('paper');
        } else {
            return redirect()->withErrors('更新失败！');
        }
    }

    public function destroy($id){
        $paper = Paper::find($id); 
        if($paper->delete()){
            return redirect('paper')->withErrors('删除成功！');
        }else{
            return redirect('paper')->withErrors('删除失败！');
        }
    }

    public function upvote($id){
        $paper = Paper::find($id);
        $paper->upvote_total += 1;
        $upvote_total = $paper->upvote_total;
        $paper->save();
        return $upvote_total;
    }

    public function userPaper(Request $request){
        $user = $request->user()->id;
        $papers = Paper::where('author', $user)->orderBy('created_at', 'desc')->get();
        // var_dump($papers);
        return $papers;
    }
}
